package com.sptpc.exam;

import com.sptpc.exam.util.DBHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/admin/saveuseredit.do")
public class SaveUserEditServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userId = request.getParameter("uid");
        int uid = Integer.valueOf(userId);
        String tname = request.getParameter("trueName");
        String major = request.getParameter("major");

        try {
            // 查询数据库 where uid = ?
            Connection conn = DBHelper.getConnection();
            String sql = "update user set true_name = ?, major = ? where uid = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, tname);
            pstmt.setString(2, major);
            pstmt.setInt(3, uid);
            pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        response.sendRedirect("usermanage.do");
    }
}
